﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Xml;

namespace CXW.Core.Sharp
{
    public static class LanguageService
    {
        static string languagePath = Path.Combine(PropertyService.DataDirectory, "languages");

        static ReadOnlyCollection<Language> languages = null;
        /// <summary>获取只读语言对象List集合</summary>
        public static ReadOnlyCollection<Language> Languages
        {
            get { return languages; }
        }
        /// <summary>私有静态构造函数</summary>
        static LanguageService()
        {
            List<Language> languages = new List<Language>();

            XmlDocument doc = new XmlDocument();
            doc.Load(Path.Combine(languagePath, "LanguageDefinition.xml"));

            XmlNodeList nodes = doc.DocumentElement.ChildNodes;

            foreach (XmlNode node in nodes)
            {
                XmlElement el = node as XmlElement;
                if (el != null)
                {
                    languages.Add(new Language(
                        el.Attributes["name"].InnerText,
                        el.Attributes["code"].InnerText,
                  (Image)ResourceService.GetObject(el.Attributes["icon"].InnerText),
                        el.GetAttribute("dir") == "rtl"
                    ));
                }
            }
            LanguageService.languages = languages.AsReadOnly();
        }

        public static Language GetLanguage(string code)
        {
            foreach (Language l in languages)
            {
                if (l.Code == code)
                    return l;
            }
            foreach (Language l in languages)
            {
                if (l.Code.StartsWith(code, StringComparison.Ordinal))
                    return l;
            }
            return languages[0];
        }
        /// <summary>获取语言编码在语言数组中的索引</summary>
        /// <param name="code">语言编码</param>
        /// <returns>返回索引,没有找到时返回0</returns>
        internal static int GetLanguageIndex(string code)
        {
            int _index = 0;
            foreach (Language l in languages)
            {
                if (l.Code == code)
                    return _index;
                _index++;
            }
            foreach (Language l in languages)
            {
                if (l.Code.StartsWith(code, StringComparison.Ordinal))
                    return _index;
                _index++;
            }
            return 0;
        }
        /// <summary>
        /// Ensures that the active language exists
        /// </summary>
        public static void ValidateLanguage()
        {
            ResourceService.Language = GetLanguage(ResourceService.Language).Code;
        }
    }

    public class Language
    {
        public string Name { get; private set; }

        public string Code { get; private set; }

        public Image Image { get; private set; }

        public bool IsRightToLeft { get; private set; }

        internal Language(string name, string code, Image imagePath, bool isRightToLeft)
        {
            this.Name = name;
            this.Code = code;
            this.Image = imagePath;
            this.IsRightToLeft = isRightToLeft;
        }
    }
}
